var api = require('../../../utils/api.js')
var $ = require('../../../utils/util.js')
var regeneratorRuntime = require('../../../utils/runtime.js')
var app = getApp()

Page({
  data: {
    hasMore: true,// 判断 数据是否全部加载完
    isLodingLock: true, //锁 防止数据加载
    pageList: [], //分页数据列表
    pageNum: 1, //页码
    isShowKong: false, // 是否是空
    keyword: '', // 搜索关键字
    imgUrl: api.url, // 图片url
    type:'0,1,2',//任务类型
    currentTab: 0, // 当前选中的tab
    nav: [
      { name: '新任务', type: 0, num: 0, color: '#dd524d', icon: 'icon-chupiao' },
      { name: '进行中', type: 1, num: 0, color: '#f5a623', icon: 'icon-ziyuan' },
      { name: '已完成', type: 2, num: 0, color: '#16304f', icon: 'icon-tick' },
    ], // tab 
  },

  onLoad: function (options) {
   // this.getPagServer(1, true) //获取分页数据  
    app._total({ type: 7, is_all: 1 }).then((res) => {
      var nav = this.data.nav
      nav[0].num = res.data.un_deal
      nav[1].num = res.data.to_deal
      nav[2].num = res.data.all_deal 
      this.setData({ nav })
    })
  },
  onShow: function (){
    this.getPagServer(1, true) //获取分页数据  
    app._total({ type: 7, is_all: 1 }).then((res) => {
      var nav = this.data.nav
      nav[0].num = res.data.un_deal
      nav[1].num = res.data.to_deal
      nav[2].num = res.data.all_deal 
      this.setData({ nav })
    })
  },

  onReachBottom() { //滑动到底部触发
    this.loadingMore()
  },

  onPullDownRefresh: function () { // 下拉刷新
    this.getPagServer(1, true) //获取分页数据

    app._total({ type: 4, is_all: 1 }).then((res) => {
      var nav = this.data.nav
      nav[0].num = res.data.un_deal
      nav[1].num = res.data.to_deal
      nav[2].num = res.data.all_deal
      this.setData({ nav })
    })
    
  },


  loadingMore: function () {  // 加载更多
    console.log(this.data.isLodingLock, 111)
    if (this.data.isLodingLock) {
      this.setData({
        isLodingLock: false
      })

      this.getPagServer()
    }
  },

  getPagServer(pageNum, isClickTab = false) { //分页  
    var params = {
      pageNum: pageNum == 1 ? 1 : this.data.pageNum,
      pageSize: 10,
      status: this.data.nav[this.data.currentTab].type,
      type:this.data.type,
      keyword: this.data.keyword
    }
    if (this.data.city_code) { // 有城市编码获取城市编码
      params = { ...params, city_code: this.data.city_code }
    }
    $.getPageList(api, this, api.task.getList, params, isClickTab).then((res) => {
      wx.stopPullDownRefresh() // 接口数据请求完停止刷新
    })
  },

  //##input 输入##
  input_handle(e) {
    this.setData({
      [e.currentTarget.dataset.key]: e.detail.value
    })
  },
  //##input 输入##

  keywordConfirmHandle() { // 搜索输入框 
      this.getPagServer(1, true) //获取分页数据
  },

  tabHanlde(e) {
    var { index } = e.detail
    this.setData({
      currentTab: index
    })
    this.getPagServer(1, true) //获取分页数据
  },
  checkboxChange(e){
      var type =e.detail.value;
      var str='';
      for(var i=0;i<type.length;i++){
        if(i<type.length-1){
          str=str+type[i]+',';
        }else{
          str+=type[i];
        }
      }
      this.setData({
          type:str
      })
      this.getPagServer(1, true) //获取分页数据
      console.log('checkbox',str)
  },
  toInfoPage(e){
    var item  = $.detail(e).item;
    console.log('item',item)
    var ticket_number=item.ticket_num+'';
    var type=ticket_number.substring(0,2);
    console.log('type',type)
    switch(type){
      case 'QX':
        $.linkurl(`/pages/robManage/robManageMsg/robManageMsg?rush_id=${item.id}&station_id=${item.station_id}&ticket_num=${item.ticket_num}`)
        break;
      case 'JX':
        $.linkurl(`/pages/checkManage/checkManageMsg/checkManageMsg?check_id=${item.id}&station_id=${item.station_id}&ticket_num=${item.ticket_num}`)
        break;
      case 'XJ':
        $.linkurl(`/pages/pollingManage/pollingManageTaskDetails/pollingManageTaskDetails?id=${item.id}&station_id=${item.station_id}&ticket_num=${item.ticket_num}`)
        break;
      default:
        $.toast('此任务无法查看详情');
    }
    
  }

})